package com.example.demo.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.common.Result;
import com.example.demo.entity.Place;
import com.example.demo.mapper.PlaceMapper;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @Author: likew
 * @Date: 2022/4/20
 */

@RestController
@RequestMapping("/place")
public class PlaceController {
    @Resource
    PlaceMapper placeMapper;
    
    @PostMapping
    public Result<?> save(@RequestBody Place place) {
        place.setTime(new Date());
        placeMapper.insert(place);
        return Result.success();
    }
    
    @PutMapping
    public Result<?> update(@RequestBody Place place) {
        placeMapper.updateById(place);
        return Result.success();
    }
    
    @DeleteMapping("/{id}")
    public Result<?> update(@PathVariable Long id) {
        placeMapper.deleteById(id);
        return Result.success();
    }
    
    @GetMapping("/{id}")
    public Result<?> getById(@PathVariable Long id) {
        return Result.success(placeMapper.selectById(id));
    }
    
    @GetMapping
    public Result<?> findPage(@RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(defaultValue = "") String search) {
        LambdaQueryWrapper<Place> wrapper = Wrappers.<Place>lambdaQuery();
        if (StrUtil.isNotBlank(search)) {
            wrapper.like(Place::getAddress, search);
        }
        Page<Place> placePage = placeMapper.selectPage(new Page<>(pageNum, pageSize), wrapper);
        return Result.success(placePage);
    }
    
    @GetMapping("/success")
    public Result<?> findPageSuccess(@RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(defaultValue = "") String search) {
        LambdaQueryWrapper<Place> wrapper = Wrappers.<Place>lambdaQuery();
        if (StrUtil.isNotBlank(search)) {
            wrapper.like(Place::getAddress, search);
        }
        wrapper.eq(Place::getApproval,"批准");
        Page<Place> placePage = placeMapper.selectPage(new Page<>(pageNum, pageSize), wrapper);
        return Result.success(placePage);
    }
    @GetMapping("/fail")
    public Result<?> findPageFail(@RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(defaultValue = "") String search) {
        LambdaQueryWrapper<Place> wrapper = Wrappers.<Place>lambdaQuery();
        if (StrUtil.isNotBlank(search)) {
            wrapper.like(Place::getAddress, search);
        }
        wrapper.eq(Place::getApproval,"未批准");
        Page<Place> placePage = placeMapper.selectPage(new Page<>(pageNum, pageSize), wrapper);
        return Result.success(placePage);
    }
}
